﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmDichVu : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler ButtonClicked = null;

        DmDichVuEntity dmDichVuEntity = null;
        DmDichVuBusiness dmDichVuBusiness = new DmDichVuBusiness();

        int State = 0;

        public frmCapNhatDmDichVu(int state)
        {
            InitializeComponent();

            this.State = state;
            LoadLoaiDichVu();
        }

        public frmCapNhatDmDichVu(int state, DmDichVuEntity obj)
        {
            InitializeComponent();

            this.State = state;
            this.dmDichVuEntity = obj;
            LoadLoaiDichVu();

            lkLoaiDichVu.EditValue = dmDichVuEntity.MaLoaiDichVu;
            txtMaDichVu.Text = dmDichVuEntity.MaDichVu;
            txtTenDichVu.Text = dmDichVuEntity.TenDichVu;
            txtMoTa.Text = dmDichVuEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmDichVuEntity.TrangThaiSuDung;

            txtMaDichVu.Enabled = false;

        }

        void LoadLoaiDichVu()
        {
            lkLoaiDichVu.Properties.DataSource = LoaiPhieu.LoaiPhieuEntity();
            lkLoaiDichVu.Properties.DisplayMember = "TenLoaiPhieu";
            lkLoaiDichVu.Properties.ValueMember = "MaLoaiPhieu";
            lkLoaiDichVu.EditValue = ConstantManagerClinic.sValueMember;
        }

        private void frmCapNhatDmDichVu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkLoaiDichVu, "loại dịch vụ") == false) return;
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaDichVu, "mã dịch vụ") == false) return;

            dmDichVuEntity = new DmDichVuEntity()
            {
                MaLoaiDichVu = lkLoaiDichVu.EditValue.ToString(),
                MaDichVu = txtMaDichVu.Text,
                TenDichVu = txtTenDichVu.Text,
                MoTa = txtMoTa.Text,
                TrangThaiSuDung = chkTrangThaiSuDung.Checked
            };

            if (State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmDichVuBusiness.GetDmDichVu(txtMaDichVu.Text) != null)
                {
                    if (Validation.ValidPrimaryKey(txtMaDichVu, "mã dịch vụ") == false) return;
                }

                dmDichVuBusiness.InsertDmDichVu(dmDichVuEntity);
                Validation.ClearControl(this);
                lkLoaiDichVu.Focus();
            }
            else {
                dmDichVuBusiness.EditDmDichVu(dmDichVuEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (ButtonClicked != null) ButtonClicked(this, e);
        }

        private void frmCapNhatDmDichVu_Load(object sender, EventArgs e)
        {
            
        }

    }
}